/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ActionForm.mobClienteRegistrarActionForm;
import BE.Cliente; 
import BL.ClienteBL;
import BL.LogBL;
import BL.ModuloBL;
import BL.SeveridadBL;
import BL.TipoOperacionBL;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.LookupDispatchAction;

/**
 *
 * @author dennis
 */
public class mobClienteRegistrarAction extends LookupDispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward registrarCliente(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        boolean boolRegistrado=false;
        mobClienteRegistrarActionForm objRegistrarClienteForm = new mobClienteRegistrarActionForm(request);
        HttpSession session = request.getSession();

        String strMensajeError  = objRegistrarClienteForm.validarDatos();

        if (strMensajeError.equals(""))

        {
        Cliente objClienteBE = new Cliente(objRegistrarClienteForm);

        int intIdUsuario = (Integer) session.getAttribute("uid");

        objClienteBE.getUsuarioBySrlModificadopor().setSrlIdpersona(intIdUsuario);
        objClienteBE.getUsuarioBySrlRegistradopor().setSrlIdpersona(intIdUsuario);

        ClienteBL objClienteBL = new ClienteBL();

        boolRegistrado = objClienteBL.insertarCliente(objClienteBE);
        LogBL.GenerarLog(request, ModuloBL.modules.VENTASMOVILES.ordinal(), SeveridadBL.nivel.AVISO.ordinal(), TipoOperacionBL.tipo.CREAR.ordinal(), "Registrar Cliente", "Se registra un cliente por el movil");

        if(!boolRegistrado)
        {
            request.setAttribute("strMsjDescripcion", "No se pudo registrar al Cliente, Intentelo de nuevo");
            request.setAttribute("indicaCompletar", true);
            request.setAttribute("objRegistrarClienteForm", objRegistrarClienteForm);
            
        }
        else
        {
        request.setAttribute("strMsjDescripcion", "Cliente Registrado");
        request.setAttribute("indicaCompletar", false);
        }

        return mapping.findForward("registrarCliente");
        }
        else
        {
           request.setAttribute("strMsjDescripcion", strMensajeError);
           request.setAttribute("indicaCompletar", true);
           request.setAttribute("objRegistrarClienteForm", objRegistrarClienteForm);
           return mapping.findForward("registrarCliente");
        }
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward myAction2(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(SUCCESS);
    }

        @Override
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.Registrar", "registrarCliente");
//        map.put("button.BuscarCliente", "buscarCliente");
//        map.put("button.AgregarTodo", "agregarTodo");
//        map.put("button.Agregar", "agregar");
//        map.put("button.QuitarTodo", "quitarTodo");
//        map.put("button.Quitar", "quitar");
        return map;
    }
}